package com.controller.frontdesk;

import com.bean.frontdesk.bo.AddOrderBO;
import com.bean.frontdesk.bo.GoodEvaluationBO;
import com.bean.frontdesk.bo.OrderBO;
import com.bean.frontdesk.vo.OrdersVO;
import com.bean.vo.admin.Result;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.service.frontdesk.UserServiceImplement;
import com.utils.GsonUtils;
import org.apache.commons.io.IOUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/api/mall/order/*")
public class OrdersServlet extends HttpServlet {
    private Gson gson = GsonUtils.getGson();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace(request.getContextPath() + "/api/mall/order/", "");
        if ("getOrderByState".equals(action)){
            showMyOrders(request,response);
        }
        if ("deleteOrder".equals(action)){
            deleteOrders(request,response);
        }
        if ("pay".equals(action)){
            pay(request,response);
        }
        if ("confirmReceive".equals(action)){
            receiveGoods(request,response);
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace(request.getContextPath() + "/api/mall/order/", "");
        if ("settleAccounts".equals(action)){
            placeOrder(request,response);
        }
        if ("sendComment".equals(action)){
            evaluationGood(request,response);
        }
        if ("addOrder".equals(action)){
            addOrder(request,response);
        }
    }

    /**
     * 添加购物车和直接购买
     * @param request
     * @param response
     */
    private void addOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream,StandardCharsets.UTF_8);
        AddOrderBO addOrderBO = gson.fromJson(requestString, AddOrderBO.class);
        UserServiceImplement userServiceImplement = new UserServiceImplement();
        int i = userServiceImplement.addOrderByAddOrders(addOrderBO);
        if (i==200){
        response.getWriter().println(gson.toJson(new Result(0,null,null)));
        return;
        }
        response.getWriter().println(gson.toJson(new Result(10000,null,"添加失败")));

    }

    /**
     * 评价
     * @param request
     * @param response
     */
    private void evaluationGood(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        GoodEvaluationBO goodEvaluationBO = gson.fromJson(requestString, GoodEvaluationBO.class);
        UserServiceImplement userServiceImplement = new UserServiceImplement();
        int evaluationState = userServiceImplement.evaluation(goodEvaluationBO);
        if (evaluationState==200){
            response.getWriter().println(gson.toJson(new Result(0,null,null)));
            return;
        }
        response.getWriter().println(gson.toJson(new Result(10000,null,"评价失败")));

    }


    /**
     * 确认收货
     * @param request
     * @param response
     */
    private void receiveGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idString = request.getParameter("id");
        int id = Integer.parseInt(idString);
        UserServiceImplement userServiceImplement = new UserServiceImplement();
        int receiveGoodState = userServiceImplement.receiveGood(id);
        if (receiveGoodState==200){
            response.getWriter().println(gson.toJson(new Result(0,null,null)));
            return;
        }
        response.getWriter().println(gson.toJson(new Result(10000,null,"确认收货失败")));



    }

    /**
     * 支付
     * @param request
     * @param response
     */
    private void pay(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idString = request.getParameter("id");
        int id = Integer.parseInt(idString);
        UserServiceImplement userServiceImplement = new UserServiceImplement();
        int payState = userServiceImplement.pay(id);
        if (payState==200){
            response.getWriter().println(gson.toJson(new Result(0,null,null)));
            return;
        }
        response.getWriter().println(gson.toJson(new Result(10000,null,"付款失败")));

    }


    /**
     * 删除订单
     * @param request
     * @param response
     */
    private void deleteOrders(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idString = request.getParameter("id");
        int id = Integer.parseInt(idString);
        UserServiceImplement userServiceImplement = new UserServiceImplement();
        int deleteOrdersState = userServiceImplement.deleteOrders(id);
        if (deleteOrdersState==200){
            response.getWriter().println(gson.toJson(new Result(0,null,null)));
            return;
        }
        response.getWriter().println(gson.toJson(new Result(10000,null,"删除失败")));

    }

    /**
     * 下单
     * @param request
     * @param response
     */
    private void placeOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream);
        OrderBO orderBO = gson.fromJson(requestString, OrderBO.class);
        UserServiceImplement userServiceImplement = new UserServiceImplement();
        int placeOrderState = userServiceImplement.placeOrder(orderBO);
        if (placeOrderState==200){
            response.getWriter().println(gson.toJson(new Result(0,null,null)));
            return;
        }
        response.getWriter().println(gson.toJson(new Result(10000,null,"下单失败")));

    }

    /**
     * 展示我的订单
     * @param request
     * @param response
     */
    private void showMyOrders(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String stateString = request.getParameter("state");
        int state = Integer.parseInt(stateString);
        String nickname = request.getParameter("token");
        UserServiceImplement userServiceImplement = new UserServiceImplement();
        List<OrdersVO> ordersVOS = userServiceImplement.showMyOrders(state, nickname);
        response.getWriter().println(gson.toJson(new Result(0,ordersVOS,null)));

    }
}
